package com.jiaquyun.jcps.mode

import android.Manifest
import android.app.Activity
import android.app.Application
import android.content.Context
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.jiaquyun.jcps.app.BaseApplication
import com.jiaquyun.jcps.entity.ArticleResponse
import com.jiaquyun.jcps.entity.ConfigResponse
import com.jiaquyun.jcps.entity.PhotoResponse
import com.jiaquyun.jcps.mode.manager.AccessManager
import com.lemlin.network.HttpServiceData
import com.lemlin.network.HttpServiceResponse
import com.lemlin.network.ViewModelSubscribe
import com.tbruyelle.rxpermissions2.RxPermissions
import io.reactivex.Observable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import top.zibin.luban.Luban

open class BaseViewModel(application: Application) : AndroidViewModel(application) {

    protected val dispose: CompositeDisposable = CompositeDisposable()

    override fun onCleared() {
        if (dispose.size() > 0 && !dispose.isDisposed) {
            dispose.dispose()
        }
    }

    fun dispose(): CompositeDisposable = dispose


    private val baseManager = BaseManager()

    val photoLiveData = MutableLiveData<HttpServiceResponse<PhotoResponse>>()
    val configValueLiveData = MutableLiveData<HttpServiceResponse<ConfigResponse>>()
    val articleValueLiveData = MutableLiveData<HttpServiceResponse<ArticleResponse>>()
    val permissionsLiveData = MutableLiveData<Boolean>()

    /**
     * 上传图片
     * */
    fun uploadImage(path: String) {
        val listener = object : ViewModelSubscribe<PhotoResponse>(photoLiveData) {}
        val disposable: Disposable = baseManager.uploadImage(path, listener)
        dispose.add(disposable)
    }

    /**
     * 获取配置
     * */
    fun configValue(name: String) {
        val listener = object : ViewModelSubscribe<ConfigResponse>(configValueLiveData) {}
        val disposable: Disposable = baseManager.configValue(name, listener)
        dispose.add(disposable)
    }

    /**
     * 获取文章
     * */
    fun getArticle(id: String) {
        val listener = object : ViewModelSubscribe<ArticleResponse>(articleValueLiveData) {}
        val disposable: Disposable = baseManager.getArticle(id, listener)
        dispose.add(disposable)
    }

    /**
     * 打开权限
     * */
    fun openPermissions(context: FragmentActivity){
        val rxPermissions = RxPermissions(context)
        val disposable: Disposable = rxPermissions.request(
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_NETWORK_STATE,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.ACCESS_WIFI_STATE,
            Manifest.permission.CHANGE_WIFI_STATE,
            Manifest.permission.WAKE_LOCK,
            Manifest.permission.BLUETOOTH
        ).subscribe {
            permissionsLiveData.postValue(it)
        }
        dispose.add(disposable)
    }
}